package com.yangcangclub.commons.utils;

import java.math.BigDecimal;

/**
 * BigDecimal的计算辅助类
 * Created by 肖明明 on 2017/2/21.
 */
public class BigDecimalUtil {
    
    /*大于、小于、大于等于、小于等于、等于、加减乘除*/

    //region 大于、小于、大于等于、小于等于、等于
    /**
     * 大于
     * @return  true：A大于B，false：A小于等于B
     */
    public static boolean isGreaterThan(BigDecimal A,BigDecimal B){
        return A.compareTo(B)==1;
    }
    public static boolean isGreaterThan(String A,String B){
        return isGreaterThan(new BigDecimal(A),new BigDecimal(B));
    }
    /**
     * 小于
     * @return  true：A小于B，false：A大于等于B
     */
    public static boolean isLessThan(BigDecimal A,BigDecimal B){
        return A.compareTo(B)==-1;
    }
    public static boolean isLessThan(String A,String B){
        return isLessThan(new BigDecimal(A),new BigDecimal(B));
    }

    /**
     * 等于
     * @return true：A等于B，false：A不等于B
     */
    public static boolean isEqualTo(BigDecimal A,BigDecimal B){
        return A.compareTo(B)==0;
    }
    public static boolean isEqualTo(String A,String B){
        return isEqualTo(new BigDecimal(A),new BigDecimal(B));
    }

    /**
     * 大于等于
     * @return true：A大于等于B，false：A小于B
     */
    public static boolean isGreaterThanOrEqualTo(BigDecimal A,BigDecimal B){
        return !isLessThan(A,B);
    }
    public static boolean isGreaterThanOrEqualTo(String A,String B){
        return isGreaterThanOrEqualTo(new BigDecimal(A),new BigDecimal(B));
    }
    /**
     * 小于等于
     * @return true：A小于等于B，false：A大于B
     */
    public static boolean isLessThanOrEqualTo(BigDecimal A,BigDecimal B){
        return !isGreaterThan(A,B);
    }
    public static boolean isLessThanOrEqualTo(String A,String B){
        return isLessThanOrEqualTo(new BigDecimal(A),new BigDecimal(B));
    }
    //endregion

    /**加法*/
    public static BigDecimal add(BigDecimal a,BigDecimal b){
        BigDecimal value=a.add(b);
        value=value.setScale(2,BigDecimal.ROUND_UP);
        return value;
    }
    public static BigDecimal add(String a,String b){
        return add(new BigDecimal(a),new BigDecimal(b));
    }
    /**减法*/
    public static BigDecimal subtract(BigDecimal a,BigDecimal b){
        BigDecimal value=a.subtract(b);
        value=value.setScale(2,BigDecimal.ROUND_UP);
        return value;
    }
    public static BigDecimal subtract(String a,String b){
        return subtract(new BigDecimal(a),new BigDecimal(b));
    }
    /**乘法*/
    public static BigDecimal multiply(BigDecimal a,BigDecimal b){
        BigDecimal value=a.multiply(b);
        value=value.setScale(2,BigDecimal.ROUND_UP);
        return value;
    }
    public static BigDecimal multiply(String a,String b){
        return multiply(new BigDecimal(a),new BigDecimal(b));
    }
    /**除法*/
    public static BigDecimal divide(BigDecimal a,BigDecimal b){
        BigDecimal value=a.divide(b,4,BigDecimal.ROUND_UP);
        value=value.setScale(2,BigDecimal.ROUND_UP);
        return value;
    }
    public static BigDecimal divide(String a,String b){
        return divide(new BigDecimal(a),new BigDecimal(b));
    }
    
    

    public static void main(String[] args) {
//        BigDecimal bd=new BigDecimal("10");
//        System.out.println(bd.divide(new BigDecimal("3"),3,BigDecimal.ROUND_UP).toString());
//        System.out.println(bd.multiply(new BigDecimal("0.301111")).toString());

        BigDecimal a=new BigDecimal("10");
        BigDecimal b=new BigDecimal("0.3123445");

        String a1="10";
        String b1="0.3123445";
        
        System.out.println(add(a,b).toString());
        System.out.println(subtract(a,b).toString());
        System.out.println(multiply(a,b).toString());
        System.out.println(divide(a,b).toString());

        System.out.println(add(a1,b1).toString());
        System.out.println(subtract(a1,b1).toString());
        System.out.println(multiply(a1,b1).toString());
        System.out.println(divide(a1,b1).toString());
    }
    
}
